package com.xintonghua.bussiness.ui.user;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.BuildConfig;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.RewardAdapter;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.GoodsManageBean;
import com.xintonghua.bussiness.bean.GoodsManageResponse;
import com.xintonghua.bussiness.bean.RewardBean;
import com.xintonghua.bussiness.bean.RewardResponse;
import com.xintonghua.bussiness.ui.user.amount.WithdrawActivity;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wanxianyi
 * 时间：2018/3/5.
 * 包名：com.xintonghua.bussiness.ui.user
 */

public class MyRewardActivity extends BaseActivity implements XRecyclerView.LoadingListener {
    @BindView(R.id.tv_reward)
    TextView tvReward;
    @BindView(R.id.lv_reward)
    XRecyclerView lvReward;
    RewardAdapter adapter;
    private int page = 1;
    private RewardResponse response;
    private List<RewardBean> rewardBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_reward);
        ButterKnife.bind(this);
        initUI();
        getReward();
    }

    public void getReward() {
        httpCent.getMyBalance(this, MyState.requestTwo);
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("我的打赏");
        RefreshUtils.initList(this, lvReward, MyUtils.dip2px(this, 1), R.color.grey_bg);
        adapter = new RewardAdapter(this, rewardBeans);
        lvReward.setAdapter(adapter);
        lvReward.setLoadingListener(this);
        find();

    }

    @OnClick({R.id.tv_reward_amount})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_reward_amount:
                Bundle bundle = new Bundle();
                bundle.putString(BuildConfig.FLAVOR, tvReward.getText().toString());
                openActivity(WithdrawActivity.class, bundle);
                break;
        }
    }

    private void find() {
        httpCent.reward(page, this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:

                if (page == 1) {
                    rewardBeans.clear();
                }
                response = JSONObject.parseObject((String) data, RewardResponse.class);
                rewardBeans.addAll(response.getList());
                adapter.notifyDataSetChanged();
                RefreshUtils.onRefresh(adapter, lvReward);
                break;
            case MyState.requestTwo:
                try {
                    org.json.JSONObject obj = new org.json.JSONObject(data.toString());
                    tvReward.setText("" + obj.getString("total"));
                } catch (Exception e) {
                    e.printStackTrace();
                    tvReward.setText("");
                }
                break;

        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        find();
    }

    @Override
    public void onLoadMore() {
        if (RefreshUtils.hasNext(response, adapter, lvReward)) {
            page++;
            find();
        }
    }
}
